<!DOCTYPE html>
<html lang="zh_CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>防抖</title>
</head>
<body>
  <input type="text" id="ipt">  

  <script>
    let ipt = document.querySelector('#ipt')
    // 防抖函数
    function debounce(fn, delay=2000) {
      let timer
      return function (args) {
        clearTimeout(timer) 
        timer = setTimeout(()=> {
          fn(args)
        }, delay)
      }
    }

    let debFn = debounce(e => {
      console.log(e.target.value)
    })

    ipt.addEventListener('keyup', (e) => {
      debFn(e)
    })
  </script>
</body>
</html>